package com.iwunu.gateway.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.iwunu.gateway.cache.RedisService;
import com.iwunu.gateway.entity.GatewayDevice;
import com.iwunu.gateway.service.GatewayService;
import com.iwunu.iot.common.constant.Constant;
import com.iwunu.iot.common.constant.ResultCodeEnum;
import com.iwunu.iot.common.util.Result;

import io.swagger.annotations.Api;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(value = "/iwunu/web", tags = "网关查询接口", description = "网关查询接口")
public class GreateWayApiController {
    @Autowired
    public RedisService  redisService;
    @Autowired
    public GatewayService gatewayService;
    
    @RequestMapping(value = "/iot/getWhiteList" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result> getWhiteList(
    		@RequestHeader String gwDeviceId
    		){
    	 Result result = null ;
    	 GatewayDevice gw = gatewayService.getGatewayByUUID(gwDeviceId);
    	if(gw!=null) {
	   		 result = new Result(ResultCodeEnum.GATEWAY_EIXT.getCode(), ResultCodeEnum.GATEWAY_EIXT.getDesc(),null);
	       	 return new ResponseEntity<>(result, HttpStatus.OK);	
    	}
    	List<String> whiteList =  redisService.scan(gw.getMac()+Constant.GREATEWAY_BRANCH+"*");
    	result.setData(whiteList);
  		result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),null);
       	return new ResponseEntity<>(result, HttpStatus.OK);
    }
    
   

}
